/*
 * @Author: xiaosihan 
 * @Date: 2023-08-21 20:34:57 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-19 13:39:01
 */

import userDesignService from "@/api/services/userDesignService";
import utilsServer from "@/api/services/utilsServer";
import designDataHistory from "@/editorComponent/designDataHistory";
import editorStore from "@/editorComponent/editorStore";
import preview2DRenderer from "@/editorComponent/preview2DRenderer";
import thumb3DRenderer from "@/editorComponent/thumb3DRenderer";
import { Popover } from "antd";
import Button from "antd/es/button";
import message from "antd/es/message";
import cloneDeep from "lodash-es/cloneDeep";
import { autorun } from "mobx";
import { ReactNode, useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import styles from "./editorHerder.module.less";
import shopCarIconPNG from "./img/shopCarIcon.png";
import JoinShopCar from "./JoinShopCar/JoinShopCar";
import TextureManageModal from "./TextureManageModal/TextureManageModal";
import userStore from "@/store/userStore";


interface Iprops {
  shopCartItemCount: number,
  children?: ReactNode
}

// 用户设计器的头部
export default function EditorHerder(props: Iprops) {
  const [messageApi, contextHolder] = message.useMessage();
  const parame = useParams();
  // const [totalNum, setTotalNum] = useState(shopCarStore.shopCarlength);
  const [open, setOpen] = useState(false);
  const [joinShopCarLoading, setJoinShopCarLoading] = useState(false);
  const [saveing, setSaveing] = useState(editorStore.saveIng);
  const [isHover, setIsHover] = useState(false);

  useEffect(() => autorun(() => {
    setSaveing(editorStore.saveIng);
  }), []);

  // 保存
  const save = async () => {
    editorStore.save();
    // editorStore.save(parame.id);
  }

  return (
    <div className={styles.editorHerder}>
      {contextHolder}
      <Button
        onClick={() => {
          location.href = "/#/admin/cart"
        }}
      >
        <img className={styles.icon} src={shopCarIconPNG} alt="" />
        去购物({props.shopCartItemCount})
      </Button>
      <Popover
        open={open}
        trigger={["click"]}
        content={
          <JoinShopCar
            closeOpen={() => { setOpen(false); }}
            changeLoading={
              (loading: boolean) => { setJoinShopCarLoading(loading) }}
          />
        } placement="bottom" title="">
        <Button
          onClick={(e) => {
            const token = userStore.getState().userToken;
            if (token.accessToken) {
              setOpen(true);
            } else {
              userStore.getState().actions.setShowLogin(true);
            }
          }}
        >加入购物车</Button>
      </Popover>
      <Button
        loading={saveing}
        type="primary"
        onClick={() => save()}
      >保存</Button>

      {/* <TextureManageModal /> */}
    </div>
  );

}